var express = require('express'),
    router = express.Router(),
    tool = require('../utils/common'),
    validate = require('../utils/validate'),
    userModel = require('../db/model/user');

/* GET home page. */
router.get('/', function (req, res, next) {
    /*(new userModel()).update({
        'username': 'xiaobai-3',
        'nickname': 'xiaobai-3',
    }, {'id': 13}).then(result => {
        res.json(tool.returnArray(result, 200));
    }).catch(e => res.json(tool.errorHandle(e)));*/
    /*try {
        console.log('ds1');
        let auth = userModel.isLogin(req, res).then(result => {
            console.log('result->', result);
        }).catch(e => console.log('login-error->', e));
        console.log('auth->', auth);
        console.log('ds2');
    } catch (e) {
        console.log('error->', e.message);
    }*/

    res.render('index', {title: 'Express'});
});

/**
 * 登录
 */
router.post('/login', (req, res) => {
    try {
        //let login = userModel.isLogin();
        //if (login) tool.errorHandle('已登录，不可重复登录');

        //获取参数
        let params = req.body;//, valid = new validate();
            //validRes = valid.rules({'username': 'require', 'password': 'require'}).check(params);
        (!params || !Object.keys(params).length) && tool.throwError('提交参数为空');
        //validRes || tool.throwError(valid.getError());

        //登录操作
        (new userModel).login(params.username, params.password).then(result => {
            res.json(tool.returnArray(result, 1));
        }).catch(e => res.json(tool.errorHandle(e)));
    } catch (e) {
        res.json(tool.errorHandle(e));
    }
});


module.exports = router;
